<!DOCTYPE html>
<html lang="" dir="ltr">
  <head>
  <meta charset="utf-8" />
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>JavaScript之数据类型转换 &middot; </title>
  <meta name="description" content="数据类型转换 JavaScript 中, 变量可以赋予任何类型的值。但是运算符对数据类型是有要求的, 如果运算符发现, 运算子的类型与预期不符, 就会自动转换类..." />
  <meta property="og:locale" content="en">
<meta property="og:type" content="article">
<meta property="og:title" content="咋瓦鲁多">
<meta property="og:description" content="aether Hugo theme for blogs">
<meta property="og:url" content="https://valentina__nappi.gitee.io/blog/post/javascript/data-type-conversion/">
<meta property="og:site_name" content="咋瓦鲁多">
<meta property="og:image" content="https://valentina__nappi.gitee.io/blog/img/ogimage.jpg">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="咋瓦鲁多">
<meta name="twitter:description" content="aether Hugo theme for blogs">
<meta name="twitter:site" content="https://valentina__nappi.gitee.io/blog">
<meta name="twitter:creator" content="Joe Hutchinson">
<meta name="twitter:image" content="https://valentina__nappi.gitee.io/blog/img/ogimage.jpg">

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" />
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/katex.min.css" integrity="sha384-9eLZqc9ds8eNjO3TmqPeYcDj8n+Qfa4nuSiGYa6DjLNcv9BtN69ZIulL9+8CqC9Y" crossorigin="anonymous">
  
  
  
  
  <link href="https://valentina__nappi.gitee.io/blog/css/concated.min.css" rel="stylesheet">
  
  <style>
    body {
      background: #ecedef url("https://valentina__nappi.gitee.io/blog/img/ignasi_pattern_s.png") repeat;
    }
  </style>
</head>

  <body class="single-body">
    <nav class="nav-bar side-padding">
  <h1 class="nav-header"><a href="https://valentina__nappi.gitee.io/blog" class="nav-text">咋瓦鲁多</a></h1>
  <div class="hamburger-menu">
    <button onclick="hamburgerMenuPressed.call(this)" aria-haspopup="true" aria-expanded="false" aria-controls="menu" aria-label="Menu">
      <span></span>
      <span></span>
    </button>
    <ul id="menu" class="hamburger-menu-overlay">
      <li><a href="https://valentina__nappi.gitee.io/blog" class="hamburger-menu-overlay-link">Home</a></li>
      
      <li><a href="https://valentina__nappi.gitee.io/blog/categories/about/" class="hamburger-menu-overlay-link">about</a></li>
      
      <li><a href="https://valentina__nappi.gitee.io/blog/categories/html/" class="hamburger-menu-overlay-link">HTML</a></li>
      
      <li><a href="https://valentina__nappi.gitee.io/blog/categories/javascript/" class="hamburger-menu-overlay-link">javascript</a></li>
      
      
    </ul>
  </div>
</nav>

    <main class="content side-text-padding">
      <article class="post dropcase">
        <header class="post-header">
          <h1 class="post-title">JavaScript之数据类型转换</h1>
          <p class="post-date">Posted <time datetime="2020-07-24">Jul 24, 2020</time></p>
        </header>
        <picture class="post-figure">
          
          
          <source srcset="https://valentina__nappi.gitee.io/blog/post/javascript/data-type-conversion/_hua1641ad431df59a323c61607cf92e9a2_498161_5b1c77670177794152fbc648a55d39c6.jpg">
          <img src="https://valentina__nappi.gitee.io/blog/post/javascript/data-type-conversion/_hua1641ad431df59a323c61607cf92e9a2_498161_5b1c77670177794152fbc648a55d39c6.jpg" >
        </picture>
        <h2>Contents</h2>
          <nav id="TableOfContents">
  <ul>
    <li><a href="#数据类型的转换">数据类型的转换</a>
      <ul>
        <li><a href="#-隐式转换">· 隐式转换</a></li>
        <li><a href="#-强制转换">· 强制转换</a></li>
      </ul>
    </li>
  </ul>
</nav>
        <h1 id="数据类型">数据类型</h1>
<h2 id="数据类型的转换">数据类型的转换</h2>
<h3 id="-隐式转换">· 隐式转换</h3>
<p><strong>问题一：自增（++）是否会发生隐式转换？</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-JavaScript" data-lang="JavaScript"><span style="color:#75715e">/* 前提先补充以下知识 */</span>
<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">a</span>;
<span style="color:#a6e22e">a</span> <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>;
<span style="color:#a6e22e">a</span><span style="color:#f92672">++</span>; <span style="color:#75715e">// a = a + 1, 结果为 2
</span><span style="color:#75715e"></span><span style="color:#a6e22e">a</span><span style="color:#f92672">--</span>; <span style="color:#75715e">// a = a - 1, 结果为 0
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">a</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;1&#39;</span>;
<span style="color:#a6e22e">a</span><span style="color:#f92672">++</span>; <span style="color:#75715e">// 结果为 2, 原因是变量a的值&#39;1&#39;发生了隐式转换，由&#39;1&#39;转为1
</span><span style="color:#75715e"></span><span style="color:#66d9ef">typeof</span> <span style="color:#a6e22e">a</span>; <span style="color:#75715e">// 结果为 number
</span></code></pre></div><p><em><strong>结论 1：变量发生自增时，字符串也会隐式转换为数字。</strong></em></p>
<p><strong>问题二：赋值运算符 += 是否也会发生隐式转换？</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-JavaScript" data-lang="JavaScript"><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">a</span>;
<span style="color:#a6e22e">a</span> <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>;
<span style="color:#a6e22e">a</span> <span style="color:#f92672">+=</span> <span style="color:#ae81ff">2</span> <span style="color:#75715e">// a = a + 2, 结果为 3
</span><span style="color:#75715e"></span>
<span style="color:#a6e22e">a</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;2&#39;</span>
<span style="color:#a6e22e">a</span> <span style="color:#f92672">+=</span> <span style="color:#ae81ff">1</span> <span style="color:#75715e">// a = a + 2, 结果为 &#39;21&#39;
</span><span style="color:#75715e"></span><span style="color:#66d9ef">typeof</span> <span style="color:#a6e22e">a</span> <span style="color:#75715e">// 结果为 string
</span></code></pre></div><p><em><strong>结论 2：赋值运算符  += 不会发生隐式转换。</strong></em></p>
<h3 id="-强制转换">· 强制转换</h3>
<ol>
<li>
<p><em><strong>parseInt</strong></em></p>
<p>计算公式：数字 * 进制数 ^ （第几个位数-1）</p>
<p>例子：</p>
</li>
</ol>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-JavaScript" data-lang="JavaScript">   parseInt(<span style="color:#e6db74">&#39;123&#39;</span>, <span style="color:#ae81ff">4</span>) <span style="color:#75715e">// 1*4^2 + 2*4^1 + 3*4^0 = 27
</span><span style="color:#75715e"></span>   parseInt(<span style="color:#e6db74">&#39;cf&#39;</span>, <span style="color:#ae81ff">16</span>) <span style="color:#75715e">// 12*16^1 + 15*16^0 = 207
</span></code></pre></div>
      </article>
      
    </main>
    <nav class="end-nav side-padding">
      
      <a ontouchstart="cardPressed.call(this)" ontouchend="cardReleased.call(this)" ontouchmove="cardReleased.call(this)" 
  href="https://valentina__nappi.gitee.io/blog/post/javascript/js-basic/" class="card blog-card" rel="bookmark" >
  
  <div class="card-img-container">
    <p class="card-img-overlay">Next Article</p>
    <picture>
      
      <source srcset="https://valentina__nappi.gitee.io/blog/post/javascript/js-basic/a30fc95ba2de5cfcdf3dbd379c4216cde08f679149f227a011befacd8ed2094f.jpg">
      <img src="https://valentina__nappi.gitee.io/blog/post/javascript/js-basic/a30fc95ba2de5cfcdf3dbd379c4216cde08f679149f227a011befacd8ed2094f.jpg" class="card-img" >
    </picture>
  </div>
  
  <article class="card-body">
    <h2 class="card-title">JavaScript基础知识归纳</h2>
    <p class="card-text">js中有5种数据类型:Undefined、Null、Boolean、Number和String。Undefined类型只有一个值,即undefined,使用var声明变量,但是未对初始化的,这个变量就是...</p>
    <div class="card-subtext muted-text">
      <p>Posted <time datetime="2020-07-24 724:00">Jul 24, 2020</time></p>
      <p>#javascript </p>
    </div>
  </article>
</a>
      
      <a ontouchstart="cardPressed.call(this)" ontouchend="cardReleased.call(this)" ontouchmove="cardReleased.call(this)" 
  href="https://valentina__nappi.gitee.io/blog" class="card home-card" style="background-image: url( https://valentina__nappi.gitee.io/blog/img/grey-cloud.jpg )" rel="bookmark" >
  Home
</a>
    </nav>
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/katex.min.js" integrity="sha384-K3vbOmF2BtaVai+Qk37uypf7VrgBubhQreNQe9aGsz9lB63dIFiQVlJbr92dw2Lx" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/contrib/auto-render.min.js" integrity="sha384-kmZOZB5ObwgQnS/DuDg6TScgOiWWBiVt0plIRkZCmE6rDZGrEOQeHM5PcHi+nyqe" crossorigin="anonymous"
    onload="renderMathInElement(document.body);"></script>
  
  <script src="https://valentina__nappi.gitee.io/blog/js/core.min.js"></script>

  </body>
</html>
